<?php

if (!defined('SMF'))
	die('Hacking attempt...');

function snet_load_status_updates($profile)
{
	global $context, $smcFunc, $memberContext, $user_info;
	
	$request = $smcFunc['db_query']('','
		SELECT s.id_status, s.id_profile, s.id_user, s.type, s.date, s.body, a.id_attach, a.id_thumb
		FROM {db_prefix}snet_status_updates AS s
			LEFT JOIN {db_prefix}snet_attachments AS a ON (s.id_status = a.id_status)
		WHERE (s.id_profile = {int:profile}
			OR s.id_user = {int:profile})
			AND s.type != 6
		ORDER BY date DESC',
		array(
			'profile' => (int) $profile,
		)
	);
	
	$statuses = array();
	
	while($row = $smcFunc['db_fetch_assoc']($request))
	{
		// Load user data
		if(loadMemberData($row['id_user']))
			loadMemberContext($row['id_user']);
			
		$row['id_attach'] = empty($row['id_thumb']) ? $row['id_attach'] : array($row['id_attach'],$row['id_thumb']);
		
		// Save this status update for display.
		$statuses[$row['id_status']] = array(
			'id' => $row['id_status'],
			'date' => snet_relative_time($row['date']),
			'real_date' => timeformat($row['date'], false),
			'timestamp' => $row['date'],
			'user' => $memberContext[$row['id_user']],
			'type' => $row['type'],
			'body' => snet_parse_status_update($row['body'],$row['type'],!empty($row['id_attach']) ? $row['id_attach'] : 0),
			'like' => $context['canonical_url'] . ';status=' . $row['id_status'] . ';sa=like',
			'dislike' => $context['canonical_url'] . ';status=' . $row['id_status'] . ';sa=dislike',	
			'comment' => $context['canonical_url'] . ';status=' . $row['id_status'] . ';sa=view#post_comment' . $row['id_status'],	
			'view_comments' => $context['canonical_url'] . ';status=' . $row['id_status'] . ';sa=view#comments',	
			'delete' => $context['canonical_url'] . ';status=' . $row['id_status'] . ';sa=delete',
			'likers' => snet_fetch_likers($row['id_status']),
			'dislikers' => snet_fetch_dislikers($row['id_status']),		
			'comments' => snet_load_comments($row['id_status'],snet_count_comments($row['id_status']),2),
			'num_comments' => snet_count_comments($row['id_status']),
			'is_note' => $row['type'] == 7,
		);
		
		// If this was posted on someone else's wall, we need to get their data too.
		if($row['id_profile'] != $profile && $row['id_user'] != $row['id_profile'])
		{
			// Load user data again
			if(loadMemberData($row['id_profile']))
				loadMemberContext($row['id_profile']);	
				
			// Add it to the array...
			$statuses[$row['id_status']]['other_user'] = $memberContext[$row['id_profile']];
		}

		// Before we're actually done we want to see if we have liked or disliked this status.
		if(in_array($user_info['id'],array_keys($statuses[$row['id_status']]['likers'])))
			$statuses[$row['id_status']]['we_like'] = true;
		elseif(in_array($user_info['id'],array_keys($statuses[$row['id_status']]['dislikers'])))
			$statuses[$row['id_status']]['we_dislike'] = true;
			
		// Make sure it's not empty (as in, a type that's not supported)
		if($statuses[$row['id_status']]['body'] === false)
			unset($statuses[$row['id_status']]);	
		// Finally, if it's a note, re-work it accordingly.
		elseif($statuses[$row['id_status']]['is_note'])
		{
			$statuses[$row['id_status']]['note'] = $statuses[$row['id_status']]['body'];
			$statuses[$row['id_status']]['body'] = $statuses[$row['id_status']]['body']['body'];
		}
	}
	
	$smcFunc['db_free_result']($request);	
	
	return $statuses;
}

function snet_load_wall_events($profile,$start='',$limit = '')
{
	global $context, $smcFunc, $user_info;
	
	$request = $smcFunc['db_query']('','
		SELECT s.id_status, s.id_profile, s.id_user, s.type, s.date, s.body, a.id_attach, a.id_thumb
		FROM {db_prefix}snet_status_updates AS s
			LEFT JOIN {db_prefix}snet_attachments AS a ON (s.id_status = a.id_status)
		WHERE (s.id_profile = {int:profile}
			OR s.id_user = {int:profile})
			AND s.type = 6
		ORDER BY date DESC
		' .(!empty($limit) ? 'LIMIT ' .(!empty($start) ? '{int:start},' : '').'{int:limit}' : ''),
		array(
			'profile' => (int) $profile,
			'start' => (int) $start,	
			'limit' => (int) $limit,
		)
	);
	
	$events = array();
	
	while($row = $smcFunc['db_fetch_assoc']($request))
	{
		// Save this status update for display.
		$events[$row['id_status']] = array(
			'id' => $row['id_status'],
			'date' => snet_relative_time($row['date']),
			'real_date' => timeformat($row['date'], false),
			'timestamp' => $row['date'],
			'body' => snet_parse_status_update($row['body'],$row['type']),	
			'delete' => $context['canonical_url'] . ';status=' . $row['id_status'] . ';sa=delete',
		);
		
		$events[$row['id_status']]['note'] = $events[$row['id_status']]['body'];
		$events[$row['id_status']]['body'] = $events[$row['id_status']]['body']['body'];
	}
	
	$smcFunc['db_free_result']($request);	
	
	return $events;
}

function snet_count_wall_events($profile)
{
	global $smcFunc, $modSettings;

	$request = $smcFunc['db_query']('', '
		SELECT COUNT(*)
		FROM {db_prefix}snet_status_updates
		WHERE type = 6
			AND id_profile = {int:profile}',
		array(
			'profile' => (int) $profile,
		)
	);
	list ($num_events) = $smcFunc['db_fetch_row']($request);
	$smcFunc['db_free_result']($request);

	return $num_events;
}

function snet_parse_status_update($body,$type, $attachment = false)
{
	global $context, $smcFunc, $user_info, $modSettings, $settings, $sourcedir, $scripturl, $boardurl, $txt;
	
	// Replacements
	$replacements = array(
		array(
			'{forum_name}',
			'{scripturl}',
			'{images_url}'			
		),
		array(
			'<a href="' . $scripturl . '">'. $context['forum_name']. '</a>',
			$scripturl,
			$settings['images_url'],			
		),
	);

	// We'll need some data for everything but text.
	if($type != 1)
	{
		$data = @unserialize($body);
		$data = is_array($data) ? $data : array();
	}

	// Se what type of update this is.
	switch($type)
	{
		// Normal text. Meh, boring; format and bail out.
		case 1:
			if(in_array(1,array_keys($context['status_update_types'])))
				return '<br />' . snet_format_text($body);
			else
				return false;
			break;
		// A photo. Shiny, shiny.
		case 2:
			if(in_array(2,array_keys($context['status_update_types'])))
				return ' <span class="snet_status_explanation">' . $txt['snet_note_add_photo_' . $data['method']] . '</span><br /><div class="snet_status_extra">
				<div class="snet_status_extra_image">'.is_array($attachment) ? '<a href="' . $scripturl . '?action=dlattach;attach=' . $attachment[0] . ';type=snetimg"><img src="' . $scripturl . '?action=dlattach;attach=' . $attachment[1] . ';type=snetimg" alt="" /></a>' : '<img src="' . $scripturl . '?action=dlattach;attach=' . $attachment . ';type=snetimg" alt="" />'.'</div>
				<div class="snet_status_extra_body">
					' . snet_format_text($data['caption']) . '<br />
				</div>
				<br class="clear" />
				</div>';
			else
				return false;
			break;
		// This reminds me of Unreal Tournament... The LINK gun!	
		case 3:
			// Whatcha gonna do, watcha gonna do when I COME FOR YOU?!!!
			if(in_array(3,array_keys($context['status_update_types'])))
				return ' <span class="snet_status_explanation">' . $txt['snet_note_share_link'] . '</span><br />' . snet_format_text($data['status']) . '<div class="snet_status_extra">
				' . (!empty($data['image']) ? '<div class="snet_status_extra_image"><a href="' . $data['url'] .'" target="_blank"><img src="' . $scripturl . '?action=dlattach;attach=' . $data['image'] . ';type=snetimg" alt="" /></a></div>' : '') . '
				<div class="snet_status_extra_body snet_link_status">
				<a href="' . $data['url'] .'" target="_blank"><strong>' . $data['title'] . '</strong></a><br /><span>' . $data['description'] . '</span>
				</div>
				<br class="clear" />
				</div>';
			else
				return false;
			break;	
		// And then he said, and she said, and I said... Yeah, that's a quote.
		case 4:
			$source = !empty($data['source']) ? '<br /><br />- <em>' . $data['source'] . '</em>' : '';
			if(in_array(4,array_keys($context['status_update_types'])))
				return ' <span class="snet_status_explanation">' . $txt['snet_note_share_quote'] . '</span><br />' . snet_format_text($data['status']) . '<div class="snet_status_extra">
				<div class="snet_status_extra_body">
					' . (!in_array('quote',explode(',',$modSettings['disabledBBC'])) ? (parse_bbc('[quote]' . $data['quote'] . $source . '[/quote]',$modSettings['snet_enable_smileys'])) : ('"' . $data['quote'] . '"' . $source)) .'
				</div>
				<br class="clear" />
				</div>';
			else
				return false;
			break;	
		// Graphics that move!
		case 5:
			if(in_array(5,array_keys($context['status_update_types'])))
				return ' <span class="snet_status_explanation">' . $txt['snet_note_share_video'] . '</span><br />' . snet_format_text($data['status']) . '<div class="snet_status_extra">
				<div class="snet_status_extra_body">
					' . parse_bbc($data['url']) . '<br />
				</div>
				<br class="clear" />
				</div>';
			else
				return false;
			break;
		// Some sort of notification, it seems.
		case 6:
			// Oi, hide stuff that's disabled.
			if(
			($modSettings['snet_enable_dislike_buttons'] != 1 && ($data['note_type'] == 'dislike' || $data['note_type'] == 'dislike_comment')) || 
			(empty($modSettings['snet_enable_like_buttons']) && ($data['note_type'] == 'dislike' || $data['note_type'] == 'dislike_comment')) || 
			(empty($modSettings['snet_enable_like_buttons_comments']) && $data['note_type'] == 'like_comment') || 
			(empty($modSettings['snet_enable_dislike_buttons_comments']) && $data['note_type'] == 'dislike_comment')
			)
				return false;

			// it is be a teckst strng
			$body_string = $data['body'];
			$data['body'] = $txt[$data['body']];
			// See if there are any additional variables to replace
			if(isset($data['replacements']))
			{
				// Now waaa-aaaa-aaa-aaa-aaaait a miiinute! Is it their own status or comment or whatever? We don't need to show their name, then, do we?
				if(isset($data['replacements']['{user}']) && $data['replacements']['{user}'] == $context['member']['id'])
				{
					unset($data['replacements']['{username}']);
					$data['body'] = $txt[$body_string . '_own'];
				}
						
				foreach($data['replacements'] AS $var => $replacement)
				{
					// If this one is supposed to be run through eval (evil?!)... well, DO EEET.
					if(isset($data['evalrepl']) && in_array($var,$data['evalrepl']))
						eval('$replacement = ' . $replacement);
						
					// Go, go, go
					$data['body'] = str_replace($var,$replacement,$data['body']);
				}
			}
			// Do the global replacements
			$data['body'] = str_replace($replacements[0],$replacements[1],$data['body']);	
			
			return $data;
			break;	
		case 7:
			// it is be a teckst strng
			$data['body'] = $txt[$data['body']];
			$data['status_expl'] = $txt[$data['status_expl']];
			if(!empty($data['extra_link']))
			{
				$extra_link_data = $data['extra_link'];
				$data['extra_link'] = $txt[$extra_link_data[0]];
				foreach($extra_link_data[1] AS $extra)
					$data['extra_link'] = sprintf($data['extra_link'],$extra);
			}
			
			// See if there are any additional variables to replace
			if(isset($data['replacements']))
			{	
				foreach($data['replacements'] AS $var => $replacement)
				{
					// If this one is supposed to be run through eval (evil?!)... well, DO EEET.
					if(isset($data['evalrepl']) && in_array($var,$data['evalrepl']))
						eval('$replacement = ' . $replacement);
						
					// Go, go, go
					$data['body'] = str_replace($var,$replacement,$data['body']);
					$data['status_expl'] = str_replace($var,$replacement,$data['status_expl']);
					$data['extra_link'] = str_replace($var,$replacement,$data['extra_link']);
				}
			}
			// Do the global replacements
			$data['body'] = str_replace($replacements[0],$replacements[1],$data['body']);		
			$data['status_expl'] = str_replace($replacements[0],$replacements[1],$data['status_expl']);		
			$data['extra_link'] = str_replace($replacements[0],$replacements[1],$data['extra_link']);
			
			$data['body'] = ' <span class="snet_status_explanation">' . $data['status_expl'] . '</span><br /><div class="snet_status_extra">
			<div class="snet_status_extra_body">
				' . $data['body'] . '<br />
			</div>
			<br class="clear" />
			</div>';
			return $data;
			break;			
	}
	
	
}

function snet_validate_url($url)
{
    $url = @parse_url($url);
 
    if (!$url)
    {
        return false;
    }
 
    $url = array_map('trim', $url);
    $url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
    $path = (isset($url['path'])) ? $url['path'] : '';
 
    if ($path == '')
    {
        $path = '/';
    }
 
    $path .= (isset($url['query'])) ? "?$url[query]" : '';
 
    if (isset($url['host']) AND $url['host'] != gethostbyname($url['host']))
    {
        if (PHP_VERSION >= 5)
        {
            $headers = get_headers("$url[scheme]://$url[host]:$url[port]$path");
        }
        else
        {
            $fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
 
            if (!$fp)
            {
                return false;
            }
            fputs($fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
            $headers = fread($fp, 4096);
            fclose($fp);
        }
        $headers = (is_array($headers)) ? implode("\n", $headers) : $headers;
        return (bool)preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers);
    }
    return false;
}

function snet_load_single_status_update($status,$profile)
{
	global $context, $smcFunc, $memberContext, $user_info;
	
	$request = $smcFunc['db_query']('','
		SELECT s.id_status, s.id_profile, s.id_user, s.type, s.date, s.body, a.id_attach, a.id_thumb
		FROM {db_prefix}snet_status_updates AS s
			LEFT JOIN {db_prefix}snet_attachments AS a ON (s.id_status = a.id_status)
		WHERE s.id_status = {int:status}
		LIMIT 1',
		array(
			'status' => (int) $status,		
			'profile' => (int) $profile,
		)
	);
	
	$row = $smcFunc['db_fetch_assoc']($request);
	
		// Load user data
		if(loadMemberData($row['id_user']))
			loadMemberContext($row['id_user']);
			
		$row['id_attach'] = empty($row['id_thumb']) ? $row['id_attach'] : array($row['id_attach'],$row['id_thumb']);
		
		// Save this status update for display.
		$limit = !empty($_REQUEST['limit']) ? $_REQUEST['limit'] : 25;
		$status = array(
			'id' => $row['id_status'],
			'date' => snet_relative_time($row['date']),
			'real_date' => timeformat($row['date'], false),
			'timestamp' => $row['date'],
			'user' => $memberContext[$row['id_user']],
			'type' => $row['type'],
			'body' => snet_parse_status_update($row['body'],$row['type'],!empty($row['id_attach']) ? $row['id_attach'] : 0),
			'like' => $context['canonical_url'] . ';status=' . $row['id_status'] . ';sa=like',
			'dislike' => $context['canonical_url'] . ';status=' . $row['id_status'] . ';sa=dislike',	
			'comment' => $context['canonical_url'] . ';status=' . $row['id_status'] . ';sa=view#post_comment' . $row['id_status'],
			'view_comments' => $context['canonical_url'] . ';status=' . $row['id_status'] . ';sa=view#comments' . $row['id_status'],	
			'delete' => $context['canonical_url'] . ';status=' . $row['id_status'] . ';sa=delete',
			'likers' => snet_fetch_likers($row['id_status']),
			'dislikers' => snet_fetch_dislikers($row['id_status']),		
			'comments' => snet_load_comments($row['id_status'],snet_count_comments($row['id_status']),$limit),
			'num_comments' => snet_count_comments($row['id_status']),
			'is_note' => $row['type'] == 7,
			'view_older' => $context['canonical_url'] . ';status=' . $row['id_status'] . ';sa=view;limit=%s#comments',
			'next_limit' => $limit + 25 
		);
		$status['next_limit'] = $limit + 25 <= $status['num_comments'] ? $limit + 25 : $status['num_comments'];
		
		// If this was posted on someone else's wall, we need to get their data too.
		if($row['id_user'] != $row['id_profile'])
		{
			// Load user data again
			if(loadMemberData($row['id_profile']))
				loadMemberContext($row['id_profile']);	
				
			// Add it to the array...
			$status['other_user'] = $memberContext[$row['id_profile']];
		}

		// Before we're actually done we want to see if we have liked or disliked this status.
		if(in_array($user_info['id'],array_keys($status['likers'])))
			$status['we_like'] = true;
		elseif(in_array($user_info['id'],array_keys($status['dislikers'])))
			$status['we_dislike'] = true;
			
		// Make sure it's not empty (as in, a type that's not supported)
		if($status['body'] === false)
		{
			fatal_lang_error('snet_error_can_not_view_status');
		}
		// Finally, if it's a note, re-work it accordingly.
		elseif($status['is_note'])
		{
			$status['note'] = $status['body'];
			$status['body'] = $status['body']['body'];
		}
	
	return $status;
}

function snet_like_status_update($status,$profile)
{
	global $context, $smcFunc, $memberContext, $user_info, $sourcedir;
	
	// See if we already like/dislike it.
	$like_request = $smcFunc['db_query']('','
		SELECT id_like, id_status, id_liker, id_note, level
		FROM {db_prefix}snet_likes
		WHERE id_status = {int:status}
			AND id_comment = 0
			AND id_liker = {int:user}
		LIMIT 1',
		array(
			'status' => (int) $status,		
			'user' => (int) $user_info['id'],
		)
	);
	
	$like = $smcFunc['db_fetch_assoc']($like_request);
	
	// Get the ID of whoever posted this.
	$status_request = $smcFunc['db_query']('','
		SELECT id_user
		FROM {db_prefix}snet_status_updates
		WHERE id_status = {int:status}
		LIMIT 1',
		array(
			'status' => (int) $status,		
		)
	);
	
	list($status_owner) = $smcFunc['db_fetch_row']($status_request);
	$smcFunc['db_free_result']($status_request);
	
	// Whatever happens below, we want to remove the old note.
	$make_note = false;
	$smcFunc['db_query']('','
		DELETE FROM {db_prefix}snet_status_updates
		WHERE id_status = {int:note}',
		array(
			'note' => (int) $like['id_note'],
		)
	);		
	
	// Okay, so, if the level is 1, they've already liked this before, so go ahead and unlike it.
	if($like['level'] == 1)
	{
		$smcFunc['db_query']('','
			DELETE FROM {db_prefix}snet_likes
			WHERE id_status = {int:status}
				AND id_comment = 0
				AND id_liker = {int:user}',
			array(
				'status' => (int) $status,		
				'user' => (int) $user_info['id'],
			)
		);	
	}
	// If it's 2, they've disliked this before. We can't like and dislike at the same time, duh. Un-dislike it and like it.
	elseif($like['level'] == 2)
	{
		$smcFunc['db_query']('','
			UPDATE {db_prefix}snet_likes
			SET level = 1
			WHERE id_status = {int:status}
				AND id_comment = 0
				AND id_liker = {int:user}
				AND id_like = {int:like}',
			array(
				'status' => (int) $status,		
				'user' => (int) $user_info['id'],
				'like' => (int) $like['id_like'],
			)
		);	
		$id_like = $like['id_like'];
		$make_note = true;
	}
	// If we didn't get anything, we can simply insert a new row since they haven't liked this before.
	else
	{
		$smcFunc['db_insert']('',
			'{db_prefix}snet_likes',
			array(
				'id_status' => 'int', 'id_liker' => 'int', 'level' => 'int',
			),
			array(
				$status, $user_info['id'], 1,
			),			
			array('id_like')
		);		
		$id_like = $smcFunc['db_insert_id']('{db_prefix}snet_likes', 'id_like');
		$make_note = true;
	}
	
	if($make_note)
	{
		require_once($sourcedir . '/snet/Subs-SimpleNetwork-Post.php');

		snet_post_status_update(array(
			'body' => @serialize(array('body' => $status_owner != $profile ? 'snet_note_like_wall_post' : 'snet_note_like_status', 'note_type' => 'like', 'replacements' => array('{user}' => $status_owner, '{username}' => 'snet_fetch_username(' . $status_owner . ');', '{status}' => $status), 'evalrepl' => array('{username}'))),
			'type' => 6,
		),$user_info['id']);
		
		// Edit the inserted like and change the note ID
		$id_note = $smcFunc['db_insert_id']('{db_prefix}snet_status_updates', 'id_status');
		$smcFunc['db_query']('','
			UPDATE {db_prefix}snet_likes
			SET id_note = {int:note}
			WHERE id_like = {int:like}',
			array(
				'note' => (int) $id_note,		
				'like' => (int) $id_like,
			)
		);	
		
	}
	
	$smcFunc['db_free_result']($like_request);
}

function snet_dislike_status_update($status,$profile)
{
	global $context, $smcFunc, $user_info, $sourcedir;
	
	global $context, $smcFunc, $memberContext, $user_info, $sourcedir;
	
	// See if we already like/dislike it.
	$like_request = $smcFunc['db_query']('','
		SELECT id_like, id_status, id_liker, id_note, level
		FROM {db_prefix}snet_likes
		WHERE id_status = {int:status}
			AND id_comment = 0
			AND id_liker = {int:user}
		LIMIT 1',
		array(
			'status' => (int) $status,		
			'user' => (int) $user_info['id'],
		)
	);
	
	$like = $smcFunc['db_fetch_assoc']($like_request);
	
	// Get the ID of whoever posted this.
	$status_request = $smcFunc['db_query']('','
		SELECT id_user
		FROM {db_prefix}snet_status_updates
		WHERE id_status = {int:status}
		LIMIT 1',
		array(
			'status' => (int) $status,		
		)
	);
	
	list($status_owner) = $smcFunc['db_fetch_row']($status_request);
	$smcFunc['db_free_result']($status_request);
	
	// Whatever happens below, we want to remove the old note.
	$make_note = false;
	$smcFunc['db_query']('','
		DELETE FROM {db_prefix}snet_status_updates
		WHERE id_status = {int:note}',
		array(
			'note' => (int) $like['id_note'],
		)
	);	

	// Okay, so, if the level is 2, they've already disliked this before, so go ahead and un-dislike it.
	if($like['level'] == 2)
	{
		$smcFunc['db_query']('','
			DELETE FROM {db_prefix}snet_likes
			WHERE id_status = {int:status}
				AND id_comment = 0
				AND id_liker = {int:user}',
			array(
				'status' => (int) $status,		
				'user' => (int) $user_info['id'],
			)
		);		
	}
	// If it's 1, they've liked this before. We can't like and dislike at the same time, duh. Unlike it and dislike it.
	elseif($like['level'] == 1)
	{
		$smcFunc['db_query']('','
			UPDATE {db_prefix}snet_likes
			SET level = 1
			WHERE id_status = {int:status}
				AND id_comment = 0
				AND id_liker = {int:user}
				AND id_like = {int:like}',
			array(
				'status' => (int) $status,		
				'user' => (int) $user_info['id'],
				'like' => (int) $like['id_like'],
			)
		);	
		$id_like = $like['id_like'];
		$make_note = true;
	}
	// If we didn't get anything, we can simply insert a new row since they haven't disliked this before.
	else
	{
		$smcFunc['db_insert']('',
			'{db_prefix}snet_likes',
			array(
				'id_status' => 'int', 'id_liker' => 'int', 'level' => 'int',
			),
			array(
				$status, $user_info['id'], 2,
			),			
			array('id_like')
		);		
		$id_like = $smcFunc['db_insert_id']('{db_prefix}snet_likes', 'id_like');
		$make_note = true;
	}
	
	if($make_note)
	{
		require_once($sourcedir . '/snet/Subs-SimpleNetwork-Post.php');
		
		snet_post_status_update(array(
			'body' => @serialize(array('body' => $status_owner != $profile ? 'snet_note_dislike_wall_post' : 'snet_note_dislike_status', 'note_type' => 'dislike', 'replacements' => array('{user}' => $status_owner, '{username}' => 'snet_fetch_username(' . $status_owner . ');', '{status}' => $status), 'evalrepl' => array('{username}'))),
			'type' => 6,
		),$user_info['id']);
		
		// Edit the inserted like and change the note ID
		$id_note = $smcFunc['db_insert_id']('{db_prefix}snet_status_updates', 'id_status');
		$smcFunc['db_query']('','
			UPDATE {db_prefix}snet_likes
			SET id_note = {int:note}
			WHERE id_like = {int:like}',
			array(
				'note' => (int) $id_note,		
				'like' => (int) $id_like,
			)
		);			
		
	}
	
	$smcFunc['db_free_result']($like_request);
}

function snet_delete_status_update($status,$profile = 0,$ajax = false)
{
	global $context, $smcFunc, $user_info;
	
	if($ajax)
		$context['can_delete'] = true;
	
	// See if we're allowed to delete this.
	if(!$context['can_delete'])
	{
		$request = $smcFunc['db_query']('','
			SELECT id_user
			FROM {db_prefix}snet_status_updates
			WHERE id_status = {int:status}
			LIMIT 1',
			array(
				'status' => (int) $status,
			)
		);
	
		$status = $smcFunc['db_fetch_assoc']($request);
		
		if($status['id_user'] != $user_info['id'] && !$ajax)
			redirectexit('action=profile;u=' . $profile);
		elseif($status['id_user'] != $user_info['id'] && $ajax)
		{
			return 'false';
		}
		
		$smcFunc['db_free_result']($request);
	}
	
	// Delete it
	$smcFunc['db_query']('','
		DELETE FROM {db_prefix}snet_status_updates
		WHERE id_status = {int:status}',
		array(
			'status' => (int) $status,		
		)
	);	
	
	// Delete all comments
	$smcFunc['db_query']('','
		DELETE FROM {db_prefix}snet_comments
		WHERE id_status = {int:status}',
		array(
			'status' => (int) $status,		
		)
	);	
	
	// Delete all likes
	$smcFunc['db_query']('','
		DELETE FROM {db_prefix}snet_likes
		WHERE id_status = {int:status}',
		array(
			'status' => (int) $status,		
		)
	);	
	
	if($ajax)
		return 'true';
}

function snet_like_comment($comment,$profile)
{
	global $context, $smcFunc, $memberContext, $user_info, $sourcedir;
	
	// See if we already like/dislike it.
	$like_request = $smcFunc['db_query']('','
		SELECT id_like, id_comment, id_liker, id_note, level
		FROM {db_prefix}snet_likes
		WHERE id_comment = {int:comment}
			AND id_status = 0
			AND id_liker = {int:user}
		LIMIT 1',
		array(
			'comment' => (int) $comment,		
			'user' => (int) $user_info['id'],
		)
	);
	
	$like = $smcFunc['db_fetch_assoc']($like_request);
	
	// Get the ID of whoever posted this, as well as the ID of the status it's posted on.
	$status_request = $smcFunc['db_query']('','
		SELECT id_user, id_status
		FROM {db_prefix}snet_comments
		WHERE id_comment = {int:comment}
		LIMIT 1',
		array(
			'comment' => (int) $comment,		
		)
	);
	
	list($comment_owner, $status) = $smcFunc['db_fetch_row']($status_request);
	$smcFunc['db_free_result']($status_request);
	
	// Whatever happens below, we'll want to delete the old note.
	$make_note = false;
	$smcFunc['db_query']('','
		DELETE FROM {db_prefix}snet_status_updates
		WHERE id_status = {int:note}',
		array(
			'note' => (int) $like['id_note'],
		)
	);
	
	// Okay, so, if the level is 1, they've already liked this before, so go ahead and unlike it.
	if($like['level'] == 1)
	{
		$smcFunc['db_query']('','
			DELETE FROM {db_prefix}snet_likes
			WHERE id_comment = {int:comment}
				AND id_status = 0
				AND id_liker = {int:user}',
			array(
				'comment' => (int) $comment,		
				'user' => (int) $user_info['id'],
			)
		);		
	}
	// If it's 2, they've disliked this before. We can't like and dislike at the same time, duh. Un-dislike it and like it.
	elseif($like['level'] == 2)
	{
		$smcFunc['db_query']('','
			UPDATE {db_prefix}snet_likes
			SET level = 1
			WHERE id_comment = {int:comment}
				AND id_status = 0
				AND id_liker = {int:user}
				AND id_like = {int:like}',
			array(
				'comment' => (int) $comment,		
				'user' => (int) $user_info['id'],
				'like' => (int) $like['id_like'],
			)
		);	
		$id_like = $like['id_like'];
		$make_note = true;
	}
	// If we didn't get anything, we can simply insert a new row since they haven't liked this before.
	else
	{
		$smcFunc['db_insert']('',
			'{db_prefix}snet_likes',
			array(
				'id_comment' => 'int', 'id_liker' => 'int', 'level' => 'int',
			),
			array(
				$comment, $user_info['id'], 1,
			),			
			array('id_like')
		);		
		$id_like = $smcFunc['db_insert_id']('{db_prefix}snet_likes', 'id_like');
		$make_note = true;
	}
	
	if($make_note)
	{
		require_once($sourcedir . '/snet/Subs-SimpleNetwork-Post.php');
		
		snet_post_status_update(array(
			'body' => @serialize(array('body' => 'snet_note_like_comment', 'note_type' => 'like_comment', 'replacements' => array('{user}' => $comment_owner, '{username}' => 'snet_fetch_username(' . $comment_owner . ');', '{status}' => $status, '{comment}' => $comment), 'evalrepl' => array('{username}'))),
			'type' => 6,
		),$user_info['id']);
		
		// Edit the inserted like and change the note ID
		$id_note = $smcFunc['db_insert_id']('{db_prefix}snet_status_updates', 'id_status');
		$smcFunc['db_query']('','
			UPDATE {db_prefix}snet_likes
			SET id_note = {int:note}
			WHERE id_like = {int:like}',
			array(
				'note' => (int) $id_note,		
				'like' => (int) $id_like,
			)
		);			
		
	}
	
	$smcFunc['db_free_result']($like_request);
}

function snet_dislike_comment($comment,$profile)
{
	global $context, $smcFunc, $memberContext, $user_info, $sourcedir;
	
	// See if we already like/dislike it.
	$like_request = $smcFunc['db_query']('','
		SELECT id_like, id_comment, id_liker, id_note, level
		FROM {db_prefix}snet_likes
		WHERE id_comment = {int:comment}
			AND id_status = 0
			AND id_liker = {int:user}
		LIMIT 1',
		array(
			'comment' => (int) $comment,		
			'user' => (int) $user_info['id'],
		)
	);
	
	$like = $smcFunc['db_fetch_assoc']($like_request);
	
	// Get the ID of whoever posted this, as well as the ID of the status it's posted on.
	$status_request = $smcFunc['db_query']('','
		SELECT id_user, id_status
		FROM {db_prefix}snet_comments
		WHERE id_comment = {int:comment}
		LIMIT 1',
		array(
			'comment' => (int) $comment,		
		)
	);
	
	list($comment_owner, $status) = $smcFunc['db_fetch_row']($status_request);
	$smcFunc['db_free_result']($status_request);
	
	// Whatever happens below, we'll want to delete the old note.
	$make_note = false;
	$smcFunc['db_query']('','
		DELETE FROM {db_prefix}snet_status_updates
		WHERE id_status = {int:note}',
		array(
			'note' => (int) $like['id_note'],
		)
	);	
	
	// Okay, so, if the level is 2, they've already disliked this before, so go ahead and un-dislike it.
	if($like['level'] == 2)
	{
		$smcFunc['db_query']('','
			DELETE FROM {db_prefix}snet_likes
			WHERE id_comment = {int:comment}
				AND id_status = 0
				AND id_liker = {int:user}',
			array(
				'comment' => (int) $comment,		
				'user' => (int) $user_info['id'],
			)
		);	
	}
	// If it's 1, they've liked this before. We can't like and dislike at the same time, duh. Unlike it and dislike it.
	elseif($like['level'] == 1)
	{
		$smcFunc['db_query']('','
			UPDATE {db_prefix}snet_likes
			SET level = 2
			WHERE id_comment = {int:comment}
				AND id_status = 0
				AND id_liker = {int:user}
				AND id_like = {int:like}',
			array(
				'comment' => (int) $comment,		
				'user' => (int) $user_info['id'],
				'like' => (int) $like['id_like'],
			)
		);	
		$id_like = $like['id_like'];
		$make_note = true;
	}
	// If we didn't get anything, we can simply insert a new row since they haven't disliked this before.
	else
	{
		$smcFunc['db_insert']('',
			'{db_prefix}snet_likes',
			array(
				'id_comment' => 'int', 'id_liker' => 'int', 'level' => 'int',
			),
			array(
				$comment, $user_info['id'], 2,
			),			
			array('id_like')
		);		
		$id_like = $smcFunc['db_insert_id']('{db_prefix}snet_likes', 'id_like');
		$make_note = true;
	}
	
	if($make_note)
	{
		require_once($sourcedir . '/snet/Subs-SimpleNetwork-Post.php');
		
		snet_post_status_update(array(
			'body' => @serialize(array('body' => 'snet_note_dislike_comment', 'note_type' => 'dislike_comment', 'replacements' => array('{user}' => $comment_owner, '{username}' => 'snet_fetch_username(' . $comment_owner . ');', '{status}' => $status, '{comment}' => $comment), 'evalrepl' => array('{username}'))),
			'type' => 6,
		),$user_info['id']);
		
		// Edit the inserted like and change the note ID
		$id_note = $smcFunc['db_insert_id']('{db_prefix}snet_status_updates', 'id_status');
		$smcFunc['db_query']('','
			UPDATE {db_prefix}snet_likes
			SET id_note = {int:note}
			WHERE id_like = {int:like}',
			array(
				'note' => (int) $id_note,		
				'like' => (int) $id_like,
			)
		);			
		
	}
	
	$smcFunc['db_free_result']($like_request);
}

function snet_delete_comment($comment,$profile = 0, $ajax = false)
{
	global $context, $smcFunc, $user_info;
	
	if($ajax)
		$context['can_delete'] = true;
		
	$request = $smcFunc['db_query']('','
		SELECT id_user, id_note
		FROM {db_prefix}snet_comments
		WHERE id_comment = {int:comment}
		LIMIT 1',
		array(
			'comment' => (int) $comment,
		)
	);		
	list($id_user,$note) = $smcFunc['db_fetch_row']($request);
		
	// See if we're allowed to delete this.
	if(!$context['can_delete'])
	{
		if($id_user != $user_info['id'])
			redirectexit('action=profile;u=' . $profile);
	}
	
	// Delete it
	$smcFunc['db_query']('','
		DELETE FROM {db_prefix}snet_comments
		WHERE id_comment = {int:comment}',
		array(
			'comment' => (int) $comment,		
		)
	);	
	
	// Delete all likes
	$smcFunc['db_query']('','
		DELETE FROM {db_prefix}snet_likes
		WHERE id_comment = {int:comment}',
		array(
			'comment' => (int) $comment,		
		)
	);	
	
	// Delete the associated note.
	$smcFunc['db_query']('','
		DELETE FROM {db_prefix}snet_status_updates
		WHERE id_status = {int:status}',
		array(
			'status' => (int) $note,		
		)
	);	

	$smcFunc['db_free_result']($request);
	
	if($ajax)
		return 'true';	
}

function snet_fetch_likers($status)
{
	global $context, $smcFunc, $scripturl;
	
	// Get all people who currently like this status update.
	$request = $smcFunc['db_query']('','
		SELECT l.id_like, l.id_status, l.id_liker, l.level, mem.real_name
		FROM {db_prefix}snet_likes AS l
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = l.id_liker)		
		WHERE l.id_status = {int:status}
			AND l.level = 1',
		array(
			'status' => (int) $status,		
		)
	);
	
	$likers = array();
	
	while($row = $smcFunc['db_fetch_assoc']($request))
	{
			
		$likers[$row['id_liker']] = array(
			'like_id' => $row['id_like'],
			'member' => array(
				'id' => $row['id_liker'],
				'name' => $row['real_name'],
				'link' => '<a href="'.$scripturl.'?action=profile;u='.$row['id_liker'].'">'.$row['real_name'].'</a>',
			),
		);
	}
	
	$smcFunc['db_free_result']($request);
	
	return $likers;
}

function snet_fetch_dislikers($status)
{
	global $context, $smcFunc, $scripturl, $txt;	
	
	// Get all people who currently dislike this status update.
	$request = $smcFunc['db_query']('','
		SELECT l.id_like, l.id_status, l.id_liker, l.level, mem.real_name
		FROM {db_prefix}snet_likes AS l
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = l.id_liker)		
		WHERE l.id_status = {int:status}
			AND l.level = 2',
		array(
			'status' => (int) $status,		
		)
	);
	
	$dislikers = array();
	
	while($row = $smcFunc['db_fetch_assoc']($request))
	{
			
		$dislikers[$row['id_liker']] = array(
			'dislike_id' => $row['id_like'],
			'member' => array(
				'id' => $row['id_liker'],
				'name' => $row['real_name'],
				'link' => '<a href="'.$scripturl.'?action=profile;u='.$row['id_liker'].'">'.$row['real_name'].'</a>',
			),
		);
	}
	
	$smcFunc['db_free_result']($request);
	
	return $dislikers;
}

function snet_fetch_comment_likers($comment)
{
	global $context, $smcFunc, $scripturl, $txt;	
	
	// Get all people who currently like this comment.
	$request = $smcFunc['db_query']('','
		SELECT l.id_like, l.id_status, l.id_liker, l.level, mem.real_name
		FROM {db_prefix}snet_likes AS l
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = l.id_liker)		
		WHERE l.id_comment = {int:comment}
			AND l.level = 1',
		array(
			'comment' => (int) $comment,		
		)
	);
	
	$likers = array();
	
	while($row = $smcFunc['db_fetch_assoc']($request))
	{
			
		$likers[$row['id_liker']] = array(
			'like_id' => $row['id_like'],
			'member' => array(
				'id' => $row['id_liker'],
				'name' => $row['real_name'],
				'link' => '<a href="'.$scripturl.'?action=profile;u='.$row['id_liker'].'">'.$row['real_name'].'</a>',
			),
		);
	}
	
	$smcFunc['db_free_result']($request);
	
	return $likers;
}

function snet_fetch_comment_dislikers($comment)
{
	global $context, $smcFunc, $scripturl, $txt;	
	
	// Get all people who currently dislike this comment.
	$request = $smcFunc['db_query']('','
		SELECT l.id_like, l.id_status, l.id_liker, l.level, mem.real_name
		FROM {db_prefix}snet_likes AS l
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = l.id_liker)		
		WHERE l.id_comment = {int:comment}
			AND l.level = 2',
		array(
			'comment' => (int) $comment,		
		)
	);
	
	$dislikers = array();
	
	while($row = $smcFunc['db_fetch_assoc']($request))
	{	
			
		$dislikers[$row['id_liker']] = array(
			'dislike_id' => $row['id_like'],
			'member' => array(
				'id' => $row['id_liker'],
				'name' => $row['real_name'],
				'link' => '<a href="'.$scripturl.'?action=profile;u='.$row['id_liker'].'">'.$row['real_name'].'</a>',
			),
		);
	}
	
	$smcFunc['db_free_result']($request);
	
	return $dislikers;
}

function snet_format_text($text, $smileys = true, $cache = '')
{
	global $modSettings, $scripturl, $smcFunc, $settings;

	if (empty($modSettings['snet_enable_bbc']))
	{
		if (!empty($modSettings['snet_enable_smileys']) && $smileys)
			parsesmileys($text);
	}
	else
	$text = parse_bbc($text, (!empty($modSettings['snet_enable_smileys']) ? $smileys : false), $cache);
	
	return $text;
}

function snet_load_comments($status,$start = '',$limit = '',$single_view = false)
{
	global $context, $smcFunc, $scripturl, $txt, $memberContext, $user_info;	
	
	// Get all comments.
	$request = $smcFunc['db_query']('','
		SELECT id_comment, id_status, id_user, date, body
		FROM {db_prefix}snet_comments
		WHERE id_status = {int:status}
		ORDER BY date
		' .(!empty($limit) ? 'LIMIT ' .(!empty($start) ? '{int:start},' : '').'{int:limit}' : ''),
		array(
			'status' => (int) $status,	
			'limit' => (int) $limit,
			'start' => $single_view ? $start : ($start > 2 ? ($start-$limit) : 0),
		)
	);
	
	$comments = array();
	
	while($row = $smcFunc['db_fetch_assoc']($request))
	{
		// Load user data again
		if(loadMemberData($row['id_user']))
			loadMemberContext($row['id_user']);	
			
		$comments[$row['id_comment']] = array(
			'id' => $row['id_comment'],
			'date' => snet_relative_time($row['date']),
			'real_date' => timeformat($row['date'], false),
			'timestamp' => $row['date'],
			'user' => $memberContext[$row['id_user']],
			'body' => snet_format_text($row['body']),
			'like' => $context['canonical_url'] . ';sa=likecomment;status=' . $row['id_status'] . ';comment=' . $row['id_comment'],	
			'dislike' => $context['canonical_url'] . ';sa=dislikecomment;status=' . $row['id_status'] . ';comment=' . $row['id_comment'],		
			'delete' => $context['canonical_url'] . ';sa=deletecomment;status=' . $row['id_status'] . ';comment=' . $row['id_comment'],		
			'likers' => snet_fetch_comment_likers($row['id_comment']),
			'dislikers' => snet_fetch_comment_dislikers($row['id_comment']),			
		);
		
		// Before we're actually done we want to see if we have liked or disliked this comment.
		if(in_array($user_info['id'],array_keys($comments[$row['id_comment']]['likers'])))
			$comments[$row['id_comment']]['we_like'] = true;
		elseif(in_array($user_info['id'],array_keys($comments[$row['id_comment']]['dislikers'])))
			$comments[$row['id_comment']]['we_dislike'] = true;			
	}
	
	$smcFunc['db_free_result']($request);
	
	return $comments;
}

function snet_count_comments($status)
{
	global $context, $smcFunc, $scripturl, $txt, $memberContext;	
	
	// Count comments.
	$request = $smcFunc['db_query']('','
		SELECT COUNT(id_comment)
		FROM {db_prefix}snet_comments
		WHERE id_status = {int:status}',
		array(
			'status' => (int) $status,	
		)
	);
	
	$row = $smcFunc['db_fetch_row']($request);
	$smcFunc['db_free_result']($request);
	
	return $row[0];
}

function snet_relative_time($ptime) {
    $etime = time() - $ptime;
    
    if ($etime < 1) {
        return '0 seconds ago';
    }
    
    $a = array(			
                24 * 60 * 60            =>  'day',
                60 * 60                 =>  'hour',
                60                      =>  'minute',
                1                       =>  'second'
                );
				
	if($etime >= 432000)	
		return timeformat($ptime);
    
    foreach ($a as $secs => $str) {
        $d = $etime / $secs;
        if ($d >= 1) {
            $r = round($d);
            return $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago';
        }
    }
}

// Grab the username of a user. What'd you expect?
function snet_fetch_username($id_user)
{
	global $smcFunc;
	
	$request = $smcFunc['db_query']('','
		SELECT real_name
		FROM {db_prefix}members
		WHERE id_member = {int:id_user}
		LIMIT 1',
		array(
			'id_user' => (int) $id_user,	
		)
	);

	list($username) = $smcFunc['db_fetch_row']($request);
	$smcFunc['db_free_result']($request);

	return $username;
}

// Grab the subject of the given topic.
function snet_fetch_topic_subject($id_topic)
{
	global $smcFunc;
	
	$request = $smcFunc['db_query']('','
		SELECT msg.subject
		FROM {db_prefix}topics AS t
			LEFT JOIN {db_prefix}messages AS msg ON (msg.id_msg = t.id_first_msg)
		WHERE t.id_topic = {int:id_topic}
		LIMIT 1',
		array(
			'id_topic' => (int) $id_topic,	
		)
	);

	list($subject) = $smcFunc['db_fetch_row']($request);
	$smcFunc['db_free_result']($request);

	return $subject;
}

// Grab the body of the given message
function snet_fetch_message_body($id_message)
{
	global $smcFunc;
	
	$request = $smcFunc['db_query']('','
		SELECT msg.body
		FROM {db_prefix}messages AS msg
		WHERE msg.id_msg = {int:id_message}
		LIMIT 1',
		array(
			'id_message' => (int) $id_message,	
		)
	);

	list($body) = $smcFunc['db_fetch_row']($request);
	$smcFunc['db_free_result']($request);

	return parse_bbc(substr($body,0,100));
}

// Fetch message icon
function snet_fetch_message_icon($id_message)
{
	global $smcFunc;
	
	$request = $smcFunc['db_query']('','
		SELECT msg.icon
		FROM {db_prefix}messages AS msg
		WHERE msg.id_msg = {int:id_message}
		LIMIT 1',
		array(
			'id_message' => (int) $id_message,	
		)
	);

	list($icon) = $smcFunc['db_fetch_row']($request);
	$smcFunc['db_free_result']($request);

	return $icon;
}

function snet_allowed_to($permission = '',$profile)
{
	global $smcFunc, $user_profile, $user_info, $context, $modSettings;
	
	return true;
	
	if(empty($permission) || $user_info['is_admin'])
		return true;
		
	// We'll need their warning level to see if they can post.
	$muted = (!empty($modSettings['warning_mute']) && $modSettings['warning_mute'] <= $user_info['warning']) ? true : false;		
		
	switch($permission)
	{
		case 'comment':
			// If they're the owner of this profile, they'll only need the can_comment_own permission and they're ready to go.
			if($context['user']['is_owner'])
				return allowedTo('snet_can_comment_own');
			// If not, it's trickier.
			else
			{
				// If they're on our ignore list, or if they're muted, don't let them to shit.
				if(in_array($user_info['id'],explode(',',$user_profile[$profile]['pm_ignore_list'])) || $muted)
					return false;
				
				// So they're not ignored, then. See if they're allowed to comment, finally.
				return allowedTo('snet_can_comment_any');
			}
			break;
		case 'like':
			// If they're the owner of this profile, they'll only need the can_like_own permission and they're ready to go.
			if($context['user']['is_owner'])
				return allowedTo('snet_can_like_own');
			// If not, it's trickier.
			else
			{
				// If they're on our ignore list, or if they're muted, don't let them to shit.
				if(in_array($user_info['id'],explode(',',$user_profile[$profile]['pm_ignore_list'])) || $muted)
					return false;
				
				// So they're not ignored, then. See if they're allowed to like stuff, finally.
				return allowedTo('snet_can_like_any');
			}
			break;	
		case 'dislike':
			// If they're the owner of this profile, they'll only need the can_dislike_own permission and they're ready to go.
			if($context['user']['is_owner'])
				return allowedTo('snet_can_dislike_own');
			// If not, it's trickier.
			else
			{
				// If they're on our ignore list, or if they're muted, don't let them to shit.
				if(in_array($user_info['id'],explode(',',$user_profile[$profile]['pm_ignore_list'])) || $muted)
					return false;
				
				// So they're not ignored, then. See if they're allowed to dislike stuff, finally.
				return allowedTo('snet_can_dislike_any');
			}
			break;
		case 'post':
			// If they're the owner of this profile, they'll only need the can_post_own permission and they're ready to go.
			if($context['user']['is_owner'])
				return allowedTo('snet_can_post_own');
			// No? Is it possible to post on walls other than your own, at all?
			elseif(!empty($modSettings['snet_enable_wall_posts']))
			{
				// If they're on our ignore list, or if they're muted, don't let them to shit.
				if(in_array($user_info['id'],explode(',',$user_profile[$profile]['pm_ignore_list'])) || $muted)
					return false;
				
				// So they're not ignored, then. See if they're allowed to post on this wall.
				return allowedTo('snet_can_post_any');
			}
			break;		
		case 'delete':
			// If they're the owner of this profile, they'll only need the can_delete_own permission and they're ready to go.
			if($context['user']['is_owner'])
				return allowedTo('snet_can_delete_own');
			// Or can they delete anything?
			elseif(!empty($modSettings['snet_enable_wall_posts']))
				return allowedTo('snet_can_delete_any');
			break;				
		default:
			return true;
			break;
	}
}

function snet_is_allowed_to($permission,$profile)
{
	if(!snet_allowed_to($permission,$profile))
		fatal_lang_error('snet_error_not_allowed_to_' . $permission);
}

function snet_status_exists($status,$profile,$kick_em_out = false)
{
	global $smcFunc;
	
	$request = $smcFunc['db_query']('','
		SELECT COUNT(id_status)
		FROM {db_prefix}snet_status_updates
		WHERE id_status = {int:status}
			AND id_profile = {int:profile}',
		array(
			'status' => (int) $status,		
			'profile' => (int) $profile,
		)
	);
	
	list($count) = $smcFunc['db_fetch_row']($request);
	
	if($count != 1 && $kick_em_out)
		fatal_lang_error('snet_error_can_not_view_status');
	else
		return $count == 1 ? true : false;
}

?>